import { getInfo, getInfoById } from '@/api/user'
import { getToken, removeToken, setToken, setTokenTime } from '@/utils/auth'
import { Message } from 'element-ui'

export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    // 更新token
    updateToken(state, token) {
      state.token = token
      setTokenTime(Date.now())
      setToken(state.token)
    },
    delToken(state) {
      state.token = ''
      removeToken()
    },
    updateUserInfo(state, value) {
      state.userInfo = value
    }
  },
  actions: {
    async getUserInfo(context) {
      try {
        const res1 = await getInfo()
        // console.log(res1.data)
        const res2 = await getInfoById(res1.data.userId)
        // console.log(res2.data)
        context.commit('updateUserInfo', { ...res1.data, ...res2.data })
      } catch (error) {
        Message.error(error)
      }
    }
  }
}
